package demo.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.ReqBo.ReqUserBeanBo;
import com.example.demo.client.TxwFeignTestClient;
import demo.bean.UserBean;
import io.seata.spring.annotation.GlobalTransactional;
import io.seata.tm.api.GlobalTransaction;
import io.seata.tm.api.GlobalTransactionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.transaction.Transactional;

@RestController
@RequestMapping("/tx")
public class UserController {

    @Autowired
    private TxwFeignTestClient txwFeignTestClient;

    @GetMapping(value = "/feginTest")
    @GlobalTransactional
    @Transactional
    public String feginTest(@RequestBody  UserBean userBean) {
        ReqUserBeanBo reqUserBeanBo = JSONObject.toJavaObject((JSON) JSON.toJSON(userBean),ReqUserBeanBo.class);
        GlobalTransaction seataInfo = GlobalTransactionContext.getCurrentOrCreate();
        System.out.println("第一次事务对应的XID为："+seataInfo.getXid());
        txwFeignTestClient.feginTest(reqUserBeanBo);
        //用了消息隊列，把任務放到消息隊列，就不用管了，直接執行後面代碼
        return "2";

    }
}
